package vip.zhenzicheng.nettybasic.serializable.msgpack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;

import java.util.List;

/**
 * 基于MessagePack的解码器，反序列化
 *
 * @author zhenzicheng
 * @date 2022-06-27 15:54
 */
public class MsgPackDecoder extends MessageToMessageDecoder<ByteBuf> {
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
      throws Exception {
    final int length = msg.readableBytes();
    final byte[] array = new byte[length];
    msg.getBytes(msg.readerIndex(), array, 0, length);
    MessagePack messagePack = new MessagePack();
    out.add(messagePack.read(array, User.class));
  }
}
